#include "at32f415.h"
#include "cx_hal_general.h"
#include "tmr.h"
#ifdef USE_CX_SW_TIMER
cx_sw_timer_interface_t timer1;
#endif
cx_hal_timer_handle_t tmr10;
extern void io_control(cx_pointer_t args);
void cx_timer_intr_cb(void *TMRx)
{
    SoftHandler(&timer1);
}
void TMR_Init(void)
{
    cx_sw_timer_init(&timer1);
    crm_periph_clock_enable(CRM_TMR10_PERIPH_CLOCK, TRUE);
    nvic_priority_group_config(NVIC_PRIORITY_GROUP_2);
    NVIC_EnableIRQ(TMR1_OVF_TMR10_IRQn);
    NVIC_SetPriority(TMR1_OVF_TMR10_IRQn,2);
    tmr10.controller = TMR10;
    tmr10.clock_in = 144;

    tmr_base_init(TMR10,1000-1,288-1); //2ms period
    tmr_cnt_dir_set(TMR10, TMR_COUNT_UP);
    tmr_clock_source_div_set(TMR10, TMR_CLOCK_DIV1);

    tmr_period_buffer_enable(TMR10, TRUE);
    tmr_counter_enable(TMR10, TRUE);

    cx_hal_timer_attachInterrupt(&tmr10, cx_timer_intr_cb, CX_HAL_TIMER_INTR_UPDATE);
}

